home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-11-01 | 55.5 KB | 1,302 lines |
- Newsgroups: comp.sources.misc
- From: daveg@synaptics.com (David Gillespie)
- Subject: v24i095: gnucalc - GNU Emacs Calculator, v2.00, Part47/56
- Message-ID: <1991Nov1.183859.21224@sparky.imd.sterling.com>
- X-Md4-Signature: e51a22d50957758770096c997cfde312
- Date: Fri, 1 Nov 1991 18:38:59 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: daveg@synaptics.com (David Gillespie)
- Posting-number: Volume 24, Issue 95
- Archive-name: gnucalc/part47
- Environment: Emacs
- Supersedes: gmcalc: Volume 13, Issue 27-45
-
- ---- Cut Here and unpack ----
- #!/bin/sh
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file calc.texinfo continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 47; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test ! -f _shar_wnt_.tmp; then
- echo 'x - still skipping calc.texinfo'
- else
- echo 'x - continuing file calc.texinfo'
- sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
- X
- @node Polynomial and Multilinear Fits, Error Estimates for Fits, Linear Fits, Curve Fitting
- @subsection Polynomial and Multilinear Fits
- X
- @noindent
- To fit the data to higher-order polynomials, just type one of the
- digits @kbd{2} through @kbd{9} when prompted for a model. For example,
- we could fit the original data matrix from the previous section
- (with 13, not 14) to a parabola instead of a line by typing
- @kbd{a F 2 RET}.
- X
- @example
- 2.00000000001 x - 1.5e-12 x^2 + 2.99999999999
- @end example
- X
- Note that since the constant and linear terms are enough to fit the
- data exactly, it's no surprise that Calc chose a tiny contribution
- for @cite{x^2}. (The fact that it's not exactly zero is due only
- to roundoff error. Since our data are exact integers, we could get
- an exact answer by typing @kbd{m f} first to get fraction mode.
- Then the @cite{x^2} term would vanish altogether. Usually, though,
- the data being fitted will be approximate floats so fraction mode
- won't help.)
- X
- Doing the @kbd{a F 2} fit on the data set with 14 instead of 13
- gives a much larger @cite{x^2} contribution, as Calc bends the
- line slightly to improve the fit.
- X
- @example
- 0.142857142855 x^2 + 1.34285714287 x + 3.59999999998
- @end example
- X
- An important result from the theory of polynomial fitting is that it
- is always possible to fit @i{N} data points exactly using a polynomial
- of degree @i{N-1}, sometimes called an @dfn{interpolating polynomial}.
- Using the modified (14) data matrix, a model number of 4 gives
- a polynomial that exactly matches all five data points:
- X
- @example
- 0.04167 x^4 - 0.4167 x^3 + 1.458 x^2 - 0.08333 x + 4.
- @end example
- X
- The actual coefficients we get with a precision of 12, like
- @cite{0.0416666663588}, clearly suffer from loss of precision.
- It is a good idea to increase the working precision to several
- digits beyond what you need when you do a fitting operation.
- Or, if your data are exact, use fraction mode to get exact
- results.
- X
- You can type @kbd{i} instead of a digit at the model prompt to fit
- the data exactly to a polynomial. This just counts the number of
- columns of the data matrix to choose the degree of the polynomial
- automatically.
- X
- Fitting data ``exactly'' to high-degree polynomials is not always
- a good idea, though. High-degree polynomials have a tendency to
- wiggle uncontrollably in between the fitting data points. Also,
- if the exact-fit polynomial is going to be used to interpolate or
- extrapolate the data, it is numerically better to use the @kbd{a p}
- command described below. @xref{Interpolation}.
- X
- @tex
- \bigskip
- @end tex
- X
- Another generalization of the linear model is to assume the
- @cite{y} values are a sum of linear contributions from several
- @cite{x} values. This is a @dfn{multilinear} fit, and it is also
- selected by the @kbd{1} digit key. (Calc decides whether the fit
- is linear or multilinear by counting the rows in the data matrix.)
- X
- Given the data matrix,
- X
- @group
- @example
- [ [ 1, 2, 3, 4, 5 ]
- X [ 7, 2, 3, 5, 2 ]
- X [ 14.5, 15, 18.5, 22.5, 24 ] ]
- @end example
- @end group
- X
- @noindent
- the command @kbd{a F 1 RET} will call the first row @cite{x} and the
- second row @cite{y}, and will fit the values in the third row to the
- model @cite{a + b x + c y}.
- X
- @example
- 8. + 3. x + 0.5 y
- @end example
- X
- Calc can do multilinear fits with any number of independent variables
- (i.e., with any number of data rows).
- X
- @tex
- \bigskip
- @end tex
- X
- Yet another variation is @dfn{homogeneous} linear models, in which
- the constant term is known to be zero. In the linear case, this
- means the model formula is simply @cite{a x}; in the multilinear
- case, the model might be @kbd{a x + b y + c z}; and in the polynomial
- case, the model could be @kbd{a x + b x^2 + c x^3}. You can get
- a homogeneous linear or multilinear model by pressing the letter
- @kbd{h} followed by a regular model key, like @kbd{1} or @kbd{2}.
- X
- It is certainly possible to have other constrained linear models,
- like @cite{2.3 + a x} or @kbd{a - 4 x}. While there is no single
- key to select models like these, a later section shows how to enter
- any desired model by hand. In the first case, for example, you
- would enter @kbd{a F ' 2.3 + a x}.
- X
- Another class of models that will work but must be entered by hand
- are multinomial fits, e.g., @cite{a + b x + c y + d x^2 + e y^2 + f x y}.
- X
- @node Error Estimates for Fits, Standard Nonlinear Models, Polynomial and Multilinear Fits, Curve Fitting
- @subsection Error Estimates for Fits
- X
- @noindent
- @kindex H a F
- @tindex efit
- With the Hyperbolic flag, @kbd{H a F} [@code{efit}] performs the same
- fitting operation as @kbd{a F}, but reports the coefficients as error
- forms instead of plain numbers. Fitting our two data matrices (first
- with 13, then with 14) to a line with @kbd{H a F} gives the results,
- X
- @example
- 3. + 2. x
- 2.6 +/- 0.382970843103 + 2.2 +/- 0.115470053838 x
- @end example
- X
- In the first case the estimated errors are zero because the linear
- fit is perfect. In the second case, the errors are nonzero but
- moderately small, because the data are still very close to linear.
- X
- It is also possible for the @emph{input} to a fitting operation to
- contain error forms. The data values must either all include errors
- or all be plain numbers. Error forms can go anywhere but generally
- go on the numbers in the last row of the data matrix. If the last
- row contains error forms
- `@i{y_i} @t{+/-} @c{$\sigma_i$}
- @i{sigma_i}', then the @c{$\chi^2$}
- @cite{chi^2}
- statistic is now,
- X
- @ifinfo
- @example
- chi^2 = sum(((y_i - (a + b x_i)) / sigma_i)^2, i, 1, N)
- @end example
- @end ifinfo
- @tex
- \turnoffactive
- $$ \chi^2 = \sum_{i=1}^N \left(y_i - (a + b x_i) \over \sigma_i\right)^2 $$
- @end tex
- X
- @noindent
- so that data points with larger error estimates contribute less to
- the fitting operation.
- X
- If there are error forms on other rows of the data matrix, all the
- errors for a given data point are combined; the square root of the
- sum of the squares of the errors forms the @c{$\sigma_i$}
- @cite{sigma_i} used for
- the data point.
- X
- Both @kbd{a F} and @kbd{H a F} can accept error forms in the input
- matrix, although if you are concerned about error analysis you will
- probably use @kbd{H a F} so that the output also contains error
- estimates.
- X
- If the input contains error forms but all the @c{$\sigma_i$}
- @cite{sigma_i} values are
- the same, it is easy to see that the resulting fitted model will be
- the same as if the input did not have error forms at all (@c{$\chi^2$}
- @cite{chi^2}
- is simply scaled uniformly by @c{$1 / \sigma^2$}
- @cite{1 / sigma^2}, which doesn't affect
- where it has a minimum). But there @emph{will} be a difference
- in the estimated errors of the coefficients reported by @kbd{H a F}.
- X
- Consult any text on statistical modelling of data for a discussion
- of where these error estimates come from and how they should be
- interpreted.
- X
- @tex
- \bigskip
- @end tex
- X
- @kindex I a F
- @tindex xfit
- With the Inverse flag, @kbd{I a F} [@code{xfit}] produces even more
- information. The result is a vector of six items:
- X
- @enumerate
- @item
- The model formula with error forms for its coefficients or
- parameters. This is the result that @kbd{H a F} would have
- produced.
- X
- @item
- A vector of ``raw'' parameter values for the model. These are the
- polynomial coefficients or other parameters as plain numbers, in the
- same order as the parameters appeared in the final prompt of the
- @kbd{I a F} command. For polynomials of degree @cite{d}, this vector
- will have length @cite{M = d+1} with the constant term first.
- X
- @item
- The covariance matrix @cite{C} computed from the fit. This is
- an @i{M}x@i{M} symmetric matrix; the diagonal elements
- @c{$C_{jj}$}
- @cite{C_j_j} are the variances @c{$\sigma_j^2$}
- @cite{sigma_j^2} of the parameters.
- The other elements are covariances @c{$\sigma_{ij}^2$}
- @cite{sigma_i_j^2} that describe the
- correlation between pairs of parameters. (A related set of
- numbers, the @dfn{linear correlation coefficients} @c{$r_{ij}$}
- @cite{r_i_j},
- are defined as @c{$\sigma_{ij}^2 / \sigma_i \, \sigma_j$}
- @cite{sigma_i_j^2 / sigma_i sigma_j}.)
- X
- @item
- A vector of @cite{M} ``parameter filter'' functions whose
- meanings are described below. If no filters are necessary this
- will instead be an empty vector; this is always the case for the
- polynomial and multilinear fits described so far.
- X
- @item
- The value of @c{$\chi^2$}
- @cite{chi^2} for the fit, calculated by the formulas
- shown above. This gives a measure of the quality of the fit;
- statisticians consider @c{$\chi^2 \approx N - M$}
- @cite{chi^2 = N - M} to indicate a moderately good fit
- (where again @cite{N} is the number of data points and @cite{M}
- is the number of parameters).
- X
- @item
- A measure of goodness of fit expressed as a probability @cite{Q}.
- This is computed from the @code{utpc} probability distribution
- function using @c{$\chi^2$}
- @cite{chi^2} with @cite{N - M} degrees of freedom. A
- value of 0.5 implies a good fit; some texts recommend that often
- @cite{Q = 0.1} or even 0.001 can signify an acceptable fit. In
- particular, @c{$\chi^2$}
- @cite{chi^2} statistics assume the errors in your inputs
- follow a normal (Gaussian) distribution; if they don't, you may
- have to accept smaller values of @cite{Q}.
- X
- The @cite{Q} value is computed only if the input included error
- estimates. Otherwise, Calc will report the symbol @code{nan}
- for @cite{Q}. The reason is that in this case the @c{$\chi^2$}
- @cite{chi^2}
- value has effectively been used to estimate the original errors
- in the input, and thus there is no redundant information left
- over to use for a confidence test.
- @end enumerate
- X
- @node Standard Nonlinear Models, Curve Fitting Details, Error Estimates for Fits, Curve Fitting
- @subsection Standard Nonlinear Models
- X
- @noindent
- The @kbd{a F} command also accepts other kinds of models besides
- lines and polynomials. Some common models have quick single-key
- abbreviations; others must be entered by hand as algebraic formulas.
- X
- Here is a complete list of the standard models recognized by @kbd{a F}:
- X
- @table @kbd
- @item 1
- Linear or multilinear. @i{a + b x + c y + d z}.
- @item 2-9
- Polynomials. @i{a + b x + c x^2 + d x^3}.
- @item e
- Exponential. @i{a} @t{exp}@i{(b x)} @t{exp}@i{(c y)}.
- @item E
- Base-10 exponential. @i{a} @t{10^}@i{(b x)} @t{10^}@i{(c y)}.
- @item x
- Exponential (alternate notation). @t{exp}@i{(a + b x + c y)}.
- @item X
- Base-10 exponential (alternate). @t{10^}@i{(a + b x + c y)}.
- @item l
- Logarithmic. @i{a + b} @t{ln}@i{(x) + c} @t{ln}@i{(y)}.
- @item L
- Base-10 logarithmic. @i{a + b} @t{log10}@i{(x) + c} @t{log10}@i{(y)}.
- @item ^
- General exponential. @i{a b^x c^y}.
- @item p
- Power law. @i{a x^b y^c}.
- @item q
- Quadratic. @i{a + b (x-c)^2 + d (x-e)^2}.
- @item g
- Gaussian. @c{${a \over b \sqrt{2 \pi}} \exp\left( -{1 \over 2} \left( x - c \over b \right)^2 \right)$}
- @i{(a / b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2)}.
- @end table
- X
- All of these models are used in the usual way; just press the appropriate
- letter at the model prompt, and choose variable names if you wish. The
- result will be a formula as shown in the above table, with the best-fit
- values of the parameters substituted. (You may find it easier to read
- the parameter values from the vector that is placed in the trail.)
- X
- All models except Gaussian can generalize as shown to any number of
- independent variables. Also, all the built-in models have an additive
- or multiplicative parameter shown as @cite{a} in the above table
- which can be replaced by zero or one, as appropriate, by typing @kbd{h}
- before the model key.
- X
- Note that many of these models are essentially equivalent, but express
- the parameters slightly differently. For example, @cite{a b^x} and
- the other two exponential models are all algebraic rearrangements of
- each other. Also, the ``quadratic'' model is just a degree-2 polynomial
- with the parameters expressed differently. Use whichever form best
- matches the problem.
- X
- The HP-28/48 calculators support four different models for curve
- fitting, called @code{LIN}, @code{LOG}, @code{EXP}, and @code{PWR}.
- These correspond to Calc models @samp{a + b x}, @samp{a + b ln(x)},
- @samp{a exp(b x)}, and @samp{a x^b}, respectively. In each case,
- @cite{a} is what the HP-48 identifies as the ``intercept,'' and
- @cite{b} is what it calls the ``slope.''
- X
- @tex
- \bigskip
- @end tex
- X
- If the model you want doesn't appear on this list, press @kbd{'}
- (the apostrophe key) at the model prompt to enter any algebraic
- formula, such as @kbd{m x - b}, as the model. (Not all models
- will work, though---see the next section for details.)
- X
- The model can also be an equation like @cite{y = m x + b}.
- In this case, Calc thinks of all the rows of the data matrix on
- equal terms; this model effectively has two parameters
- (@cite{m} and @cite{b}) and two independent variables (@cite{x}
- and @cite{y}), with no ``dependent'' variables. Model equations
- do not need to take this @cite{y =} form. For example, the
- implicit line equation @cite{a x + b y = 1} works fine as a
- model.
- X
- When you enter a model, Calc makes an alphabetical list of all
- the variables that appear in the model. These are used for the
- default parameters, independent variables, and dependent variable
- (in that order). If you enter a plain formula (not an equation),
- Calc assumes the dependent variable does not appear in the formula
- and thus does not need a name.
- X
- For example, if the model formula has the variables @cite{a,mu,sigma,t,x},
- and the data matrix has three rows (meaning two independent variables),
- Calc will use @cite{a,mu,sigma} as the default parameters, and the
- data rows will be named @cite{t} and @cite{x}, respectively. If you
- enter an equation instead of a plain formula, Calc will use @cite{a,mu}
- as the parameters, @cite{sigma,t} as the independent variables, and
- @cite{x} as the dependent variable (so that @cite{sigma,t,x} stand for
- the three rows of the data matrix).
- X
- You can, of course, override these choices by entering something
- different at the prompt. If you leave some variables out of the list,
- those variables must have stored values and those stored values will
- be used as constants in the model. (Stored values for the parameters
- and independent variables are ignored by the @kbd{a F} command.)
- If you list only independent variables, all the remaining variables
- in the model formula will become parameters.
- X
- If there are @kbd{$} signs in the model you type, they will stand
- for parameters and all other variables (in alphabetical order)
- will be independent. Use @kbd{$} for one parameter, @kbd{$$} for
- another, and so on. Thus @kbd{$ x + $$} is another way to describe
- a linear model.
- X
- If you type a @kbd{$} instead of @kbd{'} at the model prompt itself,
- Calc will take the model formula from the stack. (The data must then
- appear at the second stack level.) The same conventions are used to
- choose which variables in the formula are independent by default and
- which are parameters.
- X
- Models taken from the stack can also be expressed as vectors of
- two or three elements, @samp{[@var{model}, @var{vars}]} or
- @cite{[@var{model}, @var{vars}, @var{params}]}. Each of @var{vars}
- and @var{params} may be either variables or vectors of variables.
- (If @var{params} is omitted, all variables in @var{model} except
- those listed as @var{vars} are parameters.)@refill
- X
- When you enter a model manually with @kbd{'}, Calc puts a 3-vector
- describing the model in the trail so you can get it back if you wish.
- X
- @tex
- \bigskip
- @end tex
- X
- @vindex Model1
- @vindex Model2
- Finally, you can store a model in one of the Calc variables
- @code{Model1} or @code{Model2}, then use this model by typing
- @kbd{a F u} or @kbd{a F U} (respectively). The value stored in
- the variable can be any of the formats that @kbd{a F $} would
- accept for a model on the stack.
- X
- @tex
- \bigskip
- @end tex
- X
- Calc uses the principal values of inverse functions like @code{ln}
- and @code{arcsin} when doing fits. For example, when you enter
- the model @samp{y = sin(a t + b)} Calc actually uses the easier
- form @samp{arcsin(y) = a t + b}. The @code{arcsin} function always
- returns results in the range from -90 to 90 degrees (or the
- equivalent range in radians). Suppose you had data that you
- believed to represent roughly three oscillations of a sine wave,
- so that the argument of the sine might go from zero to 3*360 degrees.
- The above model would appear to be a good way to determine the
- true frequency and phase of the sine wave, but in practice it
- would fail utterly. The righthand side of the actual model
- @samp{arcsin(y) = a t + b} will grow smoothly with @cite{t}, but
- the lefthand side will bounce back and forth between -90 and 90.
- No values of @cite{a} and @cite{b} can make the two sides match,
- even approximately.
- X
- There is no good solution to this problem at present. You could
- restrict your data to small enough ranges so that the above problem
- doesn't occur (i.e., not straddling any peaks in the sine wave).
- Or, in this case, you could use a totally different method such as
- Fourier analysis, which is beyond the scope of the @kbd{a F} command.
- (Unfortunately, Calc does not currently have any facilities for
- taking Fourier and related transforms.)
- X
- @node Curve Fitting Details, Interpolation, Standard Nonlinear Models, Curve Fitting
- @subsection Curve Fitting Details
- X
- @noindent
- Calc's internal least-squares fitter can only handle multilinear
- models. More precisely, it can handle any model of the form
- @cite{a f(x,y,z) + b g(x,y,z) + c h(x,y,z)}, where @cite{a,b,c}
- are the parameters and @cite{x,y,z} are the independent variables
- (of course there can be any number of each, not just three).
- X
- In a simple multilinear or polynomial fit, it is easy to see how
- to convert the model into this form. For example, if the model
- is @cite{a + b x + c x^2}, then @cite{f(x) = 1}, @cite{g(x) = x},
- and @cite{h(x) = x^2} are suitable functions.
- X
- For other models, Calc uses a variety of algebraic manipulations
- to try to put the problem into the form
- X
- @smallexample
- Y(x,y,z) = A(a,b,c) F(x,y,z) + B(a,b,c) G(x,y,z) + C(a,b,c) H(x,y,z)
- @end smallexample
- X
- @noindent
- where @cite{Y,A,B,C,F,G,H} are arbitrary functions. It computes
- @cite{Y}, @cite{F}, @cite{G}, and @cite{H} for all the data points,
- does a standard linear fit to find the values of @cite{A}, @cite{B},
- and @cite{C}, then uses the equation solver to solve for @cite{a,b,c}
- in terms of @cite{A,B,C}.
- X
- A remarkable number of models can be cast into this general form.
- We'll look at two examples here to see how it works. The power-law
- model @cite{y = a x^b} with two independent variables and two parameters
- can be rewritten as follows:
- X
- @example
- y = a x^b
- y = a exp(b ln(x))
- y = exp(ln(a) + b ln(x))
- ln(y) = ln(a) + b ln(x)
- @end example
- X
- @noindent
- which matches the desired form with @cite{Y = ln(y)}, @cite{A = ln(a)},
- @cite{F = 1}, @cite{B = b}, and @cite{G = ln(x)}. Calc thus computes
- the logarithms of your @cite{y} and @cite{x} values, does a linear fit
- for @cite{A} and @cite{B}, then solves to get @cite{a = exp(A)} and
- @cite{b = B}.
- X
- Another interesting example is the ``quadratic'' model, which can
- be handled by expanding according to the distributive law.
- X
- @example
- y = a + b*(x - c)^2
- y = a + b c^2 - 2 b c x + b x^2
- @end example
- X
- @noindent
- which matches with @cite{Y = y}, @cite{A = a + b c^2}, @cite{F = 1},
- @cite{B = -2 b c}, @cite{G = x} (the -2 factor could just as easily
- have been put into @cite{G} instead of @cite{B}), @cite{C = b}, and
- @cite{H = x^2}.
- X
- The Gaussian model looks quite complicated, but a closer examination
- shows that it's actually similar to the quadratic model but with an
- exponential that can be brought to the top and moved into @cite{Y}.
- X
- An example of a model that cannot be put into general linear
- form is a Gaussian with a constant background added on, i.e.,
- @cite{d} + the regular Gaussian formula. If you have a model like
- this, your best bet is to replace enough of your parameters with
- constants to make the model linearizable, then adjust the constants
- manually by doing a series of fits. You can compare the fits by
- graphing them, by examining the goodness-of-fit measures returned by
- @kbd{I a F}, or by some other method suitable to your application.
- Note that some models can be linearized in several ways. The
- Gaussian-plus-@i{d} model can be linearized by setting @cite{d}
- (the background) to a constant, or by setting @cite{b} (the standard
- deviation) and @cite{c} (the mean) to constants.
- X
- To fit a model with constants substituted for some parameters, just
- store suitable values in those parameter variables, then omit them
- from the list of parameters when you answer the variables prompt.
- X
- @tex
- \bigskip
- @end tex
- X
- A last desperate step would be to use the general-purpose
- @code{minimize} function rather than @code{fit}. After all, both
- functions solve the problem of minimizing an expression (the @c{$\chi^2$}
- @cite{chi^2}
- sum) by adjusting certain parameters in the expression. The @kbd{a F}
- command is able to use a vastly more efficient algorithm due to its
- special knowledge about linear chi-square sums, but the @kbd{a N}
- command can do the same thing by brute force.
- X
- A compromise would be to pick out a few parameters without which the
- fit is linearizable, and use @code{minimize} on a call to @code{fit}
- which efficiently takes care of the rest of the parameters. The thing
- to be minimized would be the value of @c{$\chi^2$}
- @cite{chi^2} returned as
- the fifth result of the @code{xfit} function:
- X
- @smallexample
- minimize(xfit(gaus(a,b,c,d,x), x, [a,b,c], data)_5, d, guess)
- @end smallexample
- X
- @noindent
- where @code{gaus} represents the Gaussian model with background,
- @code{data} represents the data matrix, and @code{guess} represents
- the initial guess for @cite{d} that @code{minimize} requires.
- This operation will only be, shall we say, extraordinarily slow
- rather than astronomically slow (as would be the case if @code{minimize}
- were used by itself to solve the problem).
- X
- @tex
- \bigskip
- @end tex
- X
- The @kbd{I a F} [@code{xfit}] command is somewhat trickier when
- nonlinear models are used. The second item in the result is the
- vector of ``raw'' parameters @cite{A}, @cite{B}, @cite{C}. The
- covariance matrix is written in terms of those raw parameters.
- The fifth item is a vector of @dfn{filter} expressions. This
- is the empty vector @samp{[]} if the raw parameters were the same
- as the requested parameters, i.e., if @cite{A = a}, @cite{B = b},
- and so on (which is always possible if the model is already linear
- in the parameters as written, e.g., for polynomial fits). If the
- parameters had to be rearranged, the fifth item is instead a vector
- of one formula per parameter in the original model. The raw
- parameters are expressed in these ``filter'' formulas as
- @samp{fitdummy(1)} for @cite{A}, @samp{fitdummy(2)} for @cite{B},
- and so on.
- X
- When Calc needs to modify the model to return the result, it replaces
- @samp{fitdummy(1)} in all the filters with the first item in the raw
- parameters list, and so on for the other raw parameters, then
- evaluates the resulting filter formulas to get the actual parameter
- values to be substituted into the original model. In the case of
- @kbd{H a F} and @kbd{I a F} where the parameters must be error forms,
- Calc uses the square roots of the diagonal entries of the covariance
- matrix as error values for the raw parameters, then lets Calc's
- standard error-form arithmetic take it from there.
- X
- If you use @kbd{I a F} with a nonlinear model, be sure to remember
- that the covariance matrix is in terms of the raw parameters,
- @emph{not} the actual requested parameters. It's up to you to
- figure out how to interpret the covariances in the presence of
- nontrivial filter functions.
- X
- Things are also complicated when the input contains error forms.
- Suppose there are three independent and dependent variables, @cite{x},
- @cite{y}, and @cite{z}, one or more of which are error forms in the
- data. Calc combines all the error values by taking the square root
- of the sum of the squares of the errors. It then changes @cite{x}
- and @cite{y} to be plain numbers, and makes @cite{z} into an error
- form with this combined error. The @cite{Y(x,y,z)} part of the
- linearized model is evaluated, and the result should be an error
- form. The error part of that result is used for @c{$\sigma_i$}
- @cite{sigma_i} for
- the data point. If for some reason @cite{Y(x,y,z)} does not return
- an error form, the combined error from @cite{z} is used directly
- for @c{$\sigma_i$}
- @cite{sigma_i}. Finally, @cite{z} is also stripped of its error
- for use in computing @cite{F(x,y,z)}, @cite{G(x,y,z)} and so on;
- the righthand side of the linearized model is computed in regular
- arithmetic with no error forms.
- X
- (While these rules may seem complicated, they are designed to do
- the most reasonable thing in the typical case that @cite{Y(x,y,z)}
- depends only on the dependent variable @cite{z}, and in fact is
- often simply equal to @cite{z}. For common cases like polynomials
- and multilinear models, the combined error is simply used as the
- @c{$\sigma$}
- @cite{sigma} for the data point with no further ado.)
- X
- @tex
- \bigskip
- @end tex
- X
- @vindex FitRules
- It may be the case that the model you wish to use is linearizable,
- but Calc's built-in rules are unable to figure it out. Calc uses
- its algebraic rewrite mechanism to linearize a model. The rewrite
- rules are kept in the variable @code{FitRules}. You can edit this
- variable using the @kbd{s e FitRules} command; in fact, there is
- a special @kbd{s F} command just for editing @code{FitRules}.
- @xref{Operations on Variables}.
- X
- @xref{Rewrite Rules}, for a discussion of rewrite rules.
- X
- @tindex fitvar
- @tindex fitparam
- @tindex fitmodel
- @tindex fitsystem
- @tindex fitdummy
- Calc uses @code{FitRules} as follows. First, it converts the model
- to an equation if necessary and encloses the model equation in a
- call to the function @code{fitmodel} (which is not actually defined
- by Calc; it is only used as a placeholder by the rewrite rules).
- Parameter variables are renamed to function calls @samp{fitparam(1)},
- @samp{fitparam(2)}, and so on, and independent variables are renamed
- to @samp{fitvar(1)}, @samp{fitvar(2)}, etc. The dependent variable
- is the highest-numbered @code{fitvar}. For example, the power law
- model @cite{a x^b} is converted to @cite{y = a x^b}, then to
- X
- @group
- @smallexample
- fitmodel(fitvar(2) = fitparam(1) fitvar(1)^fitparam(2))
- @end smallexample
- @end group
- X
- Calc then applies the rewrites as if by @samp{C-u 0 a r FitRules}.
- (The zero prefix means that rewriting should continue until no further
- changes are possible.)
- X
- When rewriting is complete, the @code{fitmodel} call should have
- been replaced by a @code{fitsystem} call that looks like this:
- X
- @example
- fitsystem(@var{Y}, @var{FGH}, @var{abc})
- @end example
- X
- @noindent
- where @var{Y} is a formula that describes the function @cite{Y(x,y,z)},
- @var{FGH} is the vector of formulas @cite{[F(x,y,z), G(x,y,z), H(x,y,z)]},
- and @var{abc} is the vector of parameter filters which refer to the
- raw parameters as @samp{fitdummy(1)} for @cite{A}, @samp{fitdummy(2)}
- for @cite{B}, etc. While the number of raw parameters (the length of
- the @var{FGH} vector) is usually the same as the number of original
- parameters (the length of the @var{abc} vector), this is not required.
- X
- The power law model eventually boils down to
- X
- @group
- @smallexample
- fitsystem(ln(fitvar(2)),
- X [1, ln(fitvar(1))],
- X [exp(fitdummy(1)), fitdummy(2)])
- @end smallexample
- @end group
- X
- The actual implementation of @code{FitRules} is complicated; it
- proceeds in four phases. First, common rearrangements are done
- to try to bring linear terms together and isolate functions like
- @code{exp} and @code{ln} either all the way ``out'' (so that they
- can be put into @var{Y}) or all the way ``in'' (so that they can
- be put into @var{abc} or @var{FGH}). In particular, all
- non-constant powers are converted to logs-and-exponentials form,
- and the distributive law is used to expand products of sums.
- Quotients are rewritten to use the @samp{fitinv} function, where
- @samp{fitinv(x)} represents @cite{1/x} while the @code{FitRules}
- are operating. (The use of @code{fitinv} makes recognition of
- linear-looking forms easier.) If you modify @code{FitRules}, you
- will probably only need to modify the rules for this phase.
- X
- Phase two, whose rules can actually also apply during phases one
- and three, first rewrites @code{fitmodel} to a two-argument
- form @samp{fitmodel(@var{Y}, @var{model})}, where @var{Y} is
- initially zero and @var{model} has been changed from @cite{a=b}
- to @cite{a-b} form. It then tries to peel off invertible functions
- from the outside of @var{model} and put them into @var{Y} instead,
- calling the equation solver to invert the functions. Finally, when
- this is no longer possible, the @code{fitmodel} is changed to a
- four-argument @code{fitsystem}, where the fourth argument is
- @var{model} and the @var{FGH} and @var{abc} vectors are initially
- empty. (The last vector is really @var{ABC}, corresponding to
- raw parameters, for now.)
- X
- Phase three converts a sum of items in the @var{model} to a sum
- of @samp{fitpart(@var{a}, @var{b}, @var{c})} terms which represent
- terms @samp{@var{a}*@var{b}*@var{c}} of the sum, where @var{a}
- is all factors that do not involve any variables, @var{b} is all
- factors that involve only parameters, and @var{c} is the factors
- that involve only independent variables. (If this decomposition
- is not possible, the rule set will not complete and Calc will
- complain that the model is too complex.) Then @code{fitpart}s
- with equal @var{b} or @var{c} components are merged back together
- using the distributive law in order to minimize the number of
- raw parameters needed.
- X
- Phase four moves the @code{fitpart} terms into the @var{FGH} and
- @var{ABC} vectors. Also, some of the algebraic expansions that
- were done in phase 1 are undone now to make the formulas more
- computationally efficient. Finally, it calls the solver one more
- time to convert the @var{ABC} vector to an @var{abc} vector, and
- removes the fourth @var{model} argument (which by now will be zero)
- to obtain the three-argument @code{fitsystem} that the linear
- least-squares solver wants to see.
- X
- @tindex hasfitparams
- @tindex hasfitvars
- Two functions which are useful in connection with @code{FitRules}
- are @samp{hasfitparams(x)} and @samp{hasfitvars(x)}, which check
- whether @cite{x} refers to any parameters or independent variables,
- respectively. Specifically, these functions return ``true'' if the
- argument contains any @code{fitparam} (or @code{fitvar}) function
- calls, and ``false'' otherwise. (Recall that ``true'' means a
- nonzero number, and ``false'' means zero. The actual nonzero number
- returned is the largest @var{n} from all the @samp{fitparam(@var{n})}s
- or @samp{fitvar(@var{n})}s, respectively, that appear in the formula.)
- X
- @tex
- \bigskip
- @end tex
- X
- The @code{fit} function in algebraic notation normally takes four
- arguments, @samp{fit(@var{model}, @var{vars}, @var{params}, @var{data})},
- where @var{model} is the model formula as it would be typed after
- @kbd{a F '}, @var{vars} is the independent variable or a vector of
- independent variables, @var{params} likewise gives the parameter(s),
- and @var{data} is the data matrix. Note that the length of @var{vars}
- must be equal to the number of rows in @var{data} if @var{model} is
- an equation, or one less than the number of rows if @var{model} is
- a plain formula. (Actually, a name for the dependent variable is
- allowed but will be ignored in the plain-formula case.)
- X
- If @var{params} is omitted, the parameters are all variables in
- @var{model} except those that appear in @var{vars}. If @var{vars}
- is also omitted, Calc sorts all the variables that appear in
- @var{model} alphabetically and uses the higher ones for @var{vars}
- and the lower ones for @var{params}.
- X
- Alternatively, @samp{fit(@var{modelvec}, @var{data})} is allowed
- where @var{modelvec} is a 2- or 3-vector describing the model
- and variables, as discussed previously.
- X
- If Calc is unable to do the fit, the @code{fit} function is left
- in symbolic form, ordinarily with an explanatory message. The
- message will be ``Model expression is too complex'' if the
- linearizer was unable to put the model into the required form.
- X
- The @code{efit} (corresponding to @kbd{H a F}) and @code{xfit}
- (for @kbd{I a F}) functions are completely analogous.
- X
- @node Interpolation, , Curve Fitting Details, Curve Fitting
- @subsection Polynomial Interpolation
- X
- @kindex a p
- @pindex calc-poly-interp
- @tindex polint
- The @kbd{a p} (@code{calc-poly-interp}) [@code{polint}] command does
- a polynomial interpolation at a particular @cite{x} value. It takes
- two arguments from the stack: A data matrix of the sort used by
- @kbd{a F}, and a single number which represents the desired @cite{x}
- value. Calc effectively does an exact polynomial fit as if by @kbd{a F i},
- then substitutes the @cite{x} value into the result in order to get an
- approximate @cite{y} value based on the fit. (Calc does not actually
- use @kbd{a F i}, however; it uses a direct method which is both more
- efficient and more numerically stable.)
- X
- The result of @kbd{a p} is actually a vector of two values: The @cite{y}
- value approximation, and an error measure @cite{dy} that reflects Calc's
- estimation of the probable error of the approximation at that value of
- @cite{x}. If the input @cite{x} is equal to any of the @cite{x} values
- in the data matrix, the output @cite{y} will be the corresponding @cite{y}
- value from the matrix, and the output @cite{dy} will be exactly zero.
- X
- A prefix argument of 2 causes @kbd{a p} to take two vectors (@cite{x}
- and @cite{y}) from the stack instead of one data matrix.
- X
- If @cite{x} is a vector of numbers, @kbd{a p} will return a matrix of
- interpolated results for each of those @cite{x} values. (The matrix will
- have two columns, the @cite{y} values and the @cite{dy} values.)
- If @cite{x} is a formula instead of a number, the @code{polint} function
- remains in symbolic form; use the @kbd{a "} command to expand it out to
- a formula that describes the fit in symbolic terms.
- X
- In all cases, the @kbd{a p} command leaves the data vectors or matrix
- on the stack. Only the @cite{x} value is replaced by the result.
- X
- @kindex H a p
- @tindex ratint
- The @kbd{H a p} [@code{ratint}] command does a rational function
- interpolation. It is used exactly like @kbd{a p}, except that it
- uses as its model the quotient of two polynomials. If there are
- @cite{N} data points, the numerator and denominator polynomials will
- each have degree @cite{N/2} (if @cite{N} is odd, the denominator will
- have degree one higher than the numerator).
- X
- Rational approximations have the advantage that they can accurately
- describe functions that have poles (points at which the function's value
- goes to infinity, so that the denominator polynomial of the approximation
- goes to zero). If @cite{x} corresponds to a pole of the fitted rational
- function, then the result will be a division by zero. If Infinite mode
- is enabled, the result will be @samp{[uinf, uinf]}.
- X
- There is no way to get the actual coefficients of the rational function
- used by @kbd{H a p}. (The algorithm never generates these coefficients
- explicitly, and quotients of polynomials are beyond @kbd{a F}'s
- capabilities to fit.)
- X
- @node Summations, Logical Operations, Curve Fitting, Algebra
- @section Summations
- X
- @noindent
- @cindex Summation of a series
- @kindex a +
- @pindex calc-summation
- @tindex sum
- The @kbd{a +} (@code{calc-summation}) [@code{sum}] command computes
- the sum of a formula over a certain range of indices. The formula
- is taken from the top of the stack; the command prompts for the
- name of the summation index variable, the lower limit of the
- sum (any formula), and the upper limit of the sum. If you
- enter a blank line at any of these prompts, that prompt and
- any later ones are answered by reading additional elements from
- the stack. Thus, @kbd{' k^2 RET ' k RET 1 RET 5 RET a + RET}
- produces the result 55.
- @tex
- \turnoffactive
- $$ \sum_{k=1}^5 k^2 = 55 $$
- @end tex
- X
- The choice of index variable is arbitrary, but it's best not to
- use a variable with a stored value. In particular, while
- @code{i} is often a favorite index variable, it should be avoided
- in Calc because @code{i} has the imaginary constant @cite{(0, 1)}
- as a value. If you pressed @kbd{=} on a sum over @code{i}, it would
- be changed to a nonsensical sum over the ``variable'' @cite{(0, 1)}!
- If you really want to use @code{i} as an index variable, use
- @kbd{s u i RET} first to ``unstore'' this variable.
- (@xref{Storing Variables}.)
- X
- A numeric prefix argument steps the index by that amount rather
- than by one. Thus @kbd{' a_k RET C-u -2 a + k RET 10 RET 0 RET}
- yields @samp{a_10 + a_8 + a_6 + a_4 + a_2 + a_0}. A prefix
- argument of plain @kbd{C-u} causes @kbd{a +} to prompt for the
- step value, in which case you can enter any formula or enter
- a blank line to take the step value from the stack. With the
- @kbd{C-u} prefix, @kbd{a +} can take up to five arguments from
- the stack: The formula, the variable, the lower limit, the
- upper limit, and (at the top of the stack), the step value.
- X
- Calc knows how to do certain sums in closed form. For example,
- @samp{sum(6 k^2, k, 1, n) = 2 n^3 + 3 n^2 + n}. In particular,
- this is possible if the formula being summed is polynomial or
- exponential in the index variable. Sums of logarithms are
- transformed into logarithms of products. Sums of trigonometric
- and hyperbolic functions are transformed to sums of exponentials
- and then done in closed form. Also, of course, sums in which the
- lower and upper limits are both numbers can always be evaluated
- just by grinding them out, although Calc will use closed forms
- whenever it can for the sake of efficiency.
- X
- The notation for sums in algebraic formulas is
- @samp{sum(@var{expr}, @var{var}, @var{low}, @var{high}, @var{step})}.
- If @var{step} is omitted, it defaults to one. If @var{high} is
- omitted, @var{low} is actually the upper limit and the lower limit
- is one. If @var{low} is also omitted, the limits are @samp{-inf}
- and @samp{inf}, respectively.
- X
- Infinite sums can sometimes be evaluated: @samp{sum(.5^k, k, 1, inf)}
- returns @cite{1}. This is done by evaluating the sum in closed
- form (to @samp{1. - 0.5^n} in this case), then evaluating this
- formula with @code{n} set to @code{inf}. Calc's usual rules
- for ``infinite'' arithmetic can find the answer from here. If
- infinite arithmetic yields a @samp{nan}, or if the sum cannot be
- solved in closed form, Calc leaves the @code{sum} function in
- symbolic form. @xref{Infinities}.
- X
- As a special feature, if the limits are infinite (or omitted, as
- described above) but the formula includes vectors subscripted by
- expressions that include the iteration variable, Calc narrows
- the limits to include only the range of integers which result in
- legal subscripts for the vector. For example, the sum
- @samp{sum(k [a,b,c,d,e,f,g]_(2k),k)} evaluates to @samp{b + 2 d + 3 f}.
- X
- The limits of a sum do not need to be integers. For example,
- @samp{sum(a_k, k, 0, 2 n, n)} produces @samp{a_0 + a_n + a_(2 n)}.
- Calc computes the number of iterations using the formula
- @samp{1 + (@var{high} - @var{low}) / @var{step}}, which must,
- after simplification as if by @kbd{a s}, evaluate to an integer.
- X
- If the number of iterations according to the above formula does
- not come out to an integer, the sum is illegal and will be left
- in symbolic form. However, closed forms are still supplied, and
- you are on your honor not to misuse the resulting formulas by
- substituting mismatched bounds into them. For example,
- @samp{sum(k, k, 1, 10, 2)} is invalid, but Calc will go ahead and
- evaluate the closed form solution for the limits 1 and 10 to get
- the rather dubious answer, 29.25.
- X
- If the lower limit is greater than the upper limit (assuming a
- positive step size), the result is generally zero. However,
- Calc only guarantees a zero result when the upper limit is
- exactly one step less than the lower limit, i.e., if the number
- of iterations is @i{-1}. Thus @samp{sum(f(k), k, n, n-1)} is zero
- but the sum from @samp{n} to @samp{n-2} may report a nonzero value
- if closed form solutions were used.
- X
- Calc's logical predicates like @cite{a < b} return 1 for ``true''
- and 0 for ``false.'' @xref{Logical Operations}. This can be
- used to advantage for building conditional sums. For example,
- @samp{sum(prime(k)*k^2, k, 1, 20)} is the sum of the squares of all
- prime numbers from 1 to 20; the @code{prime} predicate returns 1 if
- its argument is prime and 0 otherwise. You can read this expression
- as ``the sum of @cite{k^2}, where @cite{k} is prime.'' Indeed,
- @samp{sum(prime(k)*k^2, k)} would represent the sum of @emph{all} primes
- squared, since the limits default to plus and minus infinity, but
- there are no such sums that Calc's built-in rules can write in
- closed form.
- X
- As another example, @samp{sum((k != k_0) * f(k), k, 1, n)} is the
- sum of @cite{f(k)} for all @cite{k} from 1 to @cite{n}, excluding
- one value @cite{k_0}. Slightly more tricky is the summand
- @samp{(k != k_0) / (k - k_0)}, which is an attempt to describe
- the sum of all @cite{1/(k-k_0)} except at @cite{k = k_0}, where
- this would be a division by zero. But at @cite{k = k_0}, this
- formula works out to the indeterminate form @cite{0 / 0}, which
- Calc will not assume is zero. Better would be to use
- @samp{(k != k_0) ? 1/(k-k_0) : 0}; the @samp{? :} operator does
- an ``if-then-else'' test: This expression says, ``if @c{$k \ne k_0$}
- @cite{k != k_0},
- then @cite{1/(k-k_0)}, else zero.'' Now the formula @cite{1/(k-k_0)}
- will not even be evaluated by Calc when @cite{k = k_0}.
- X
- @cindex Alternating sums
- @kindex a -
- @pindex calc-alt-summation
- @tindex asum
- The @kbd{a -} (@code{calc-alt-summation}) [@code{asum}] command
- computes an alternating sum. Successive terms of the sequence
- are given alternating signs, with the first term (corresponding
- to the lower index value) being positive. Alternating sums
- are converted to normal sums with an extra term of the form
- @samp{(-1)^(k-@var{low})}. This formula is adjusted appropriately
- if the step value is other than one. For example, the Taylor
- series for the sine function is @samp{asum(x^k / k!, k, 1, inf, 2)}.
- (Calc cannot evaluate this infinite series, but it can approximate
- it if you replace @code{inf} with any particular odd number.)
- Calc converts this series to a regular sum with a step of one,
- namely @samp{sum((-1)^k x^(2k+1) / (2k+1)!, k, 0, inf)}.
- X
- @cindex Product of a sequence
- @kindex a *
- @pindex calc-product
- @tindex prod
- The @kbd{a *} (@code{calc-product}) [@code{prod}] command is
- the analogous way to take a product of many terms. Calc also knows
- some closed forms for products, such as @samp{prod(k, k, 1, n) = n!}.
- Conditional products can be written, e.g., @samp{prod(k^prime(k), k, 1, n)}
- or @samp{prod(prime(k) ? k : 1, k, 1, n)}.
- X
- @kindex a T
- @pindex calc-tabulate
- @tindex table
- The @kbd{a T} (@code{calc-tabulate}) [@code{table}] command
- evaluates a formula at a series of iterated index values, just
- like @code{sum} and @code{prod}, but its result is simply a
- vector of the results. For example, @samp{table(a_i, i, 1, 7, 2)}
- produces @samp{[a_1, a_3, a_5, a_7]}.
- X
- @node Logical Operations, Rewrite Rules, Summations, Algebra
- @section Logical Operations
- X
- @noindent
- The following commands and algebraic functions return true/false values,
- where 1 represents ``true'' and 0 represents ``false.'' In cases where
- a truth value is required (such as for the condition part of a rewrite
- rule, or as the condition for a @kbd{Z [ Z ]} control structure), any
- nonzero value is accepted to mean ``true.'' (Specifically, anything
- for which @code{dnonzero} returns 1 is ``true,'' and anything for
- which @code{dnonzero} returns 0 or cannot decide is assumed ``false.''
- Note that this means that @kbd{Z [ Z ]} will execute the ``then''
- portion if its condition is provably true, but it will execute the
- ``else'' portion if for any condition like @cite{a = b} that is not
- provably true, even if it might be true. Algebraic functions that
- have conditions as arguments, like @code{if} and @code{land}, remain
- unevaluated if the condition is neither provably true nor provable
- false. @xref{Declarations}.)
- X
- @kindex a =
- @pindex calc-equal-to
- @tindex eq
- @tindex =
- @tindex ==
- The @kbd{a =} (@code{calc-equal-to}) command, or @samp{eq(a,b)} function
- (which can also be written @samp{a = b} or @samp{a == b} in an algebraic
- formula) is true if @cite{a} and @cite{b} are equal, either because they
- are identical expressions, or because they are numbers which are
- numerically equal. (Thus the integer 1 is considered equal to the float
- 1.0.) If the equality of @cite{a} and @cite{b} cannot be determined,
- the comparison is left in symbolic form. Note that as a command, this
- operation pops two values from the stack and pushes back either a 1 or
- a 0, or a formula @samp{a = b} if the values' equality cannot be determined.
- X
- Many Calc commands use @samp{=} formulas to represent @dfn{equations}.
- For example, the @kbd{a S} (@code{calc-solve-for}) command rearranges
- an equation to solve for a given variable. The @kbd{a M}
- (@code{calc-map-equation}) command can be used to apply any
- function to both sides of an equation; for example, @kbd{2 a M *}
- multiplies both sides of the equation by two. Note that just
- @kbd{2 *} would not do the same thing; it would produce the formula
- @samp{2 (a = b)} which represents 2 if the equality is true or
- zero if not.
- X
- The @code{eq} function with more than two arguments (e.g., @kbd{C-u 3 a =}
- or @samp{a = b = c}) tests if all of its arguments are equal. In
- algebraic notation, the @samp{=} operator is unusual in that it is
- neither left- nor right-associative: @samp{a = b = c} is not the
- same as @samp{(a = b) = c} or @samp{a = (b = c)} (which each compare
- one variable with the 1 or 0 that results from comparing two other
- variables).
- X
- @kindex a #
- @pindex calc-not-equal-to
- @tindex neq
- @tindex !=
- The @kbd{a #} (@code{calc-not-equal-to}) command, or @samp{neq(a,b)} or
- @samp{a != b} function, is true if @cite{a} and @cite{b} are not equal.
- This also works with more than two arguments; @samp{a != b != c != d}
- tests that all four of @cite{a}, @cite{b}, @cite{c}, and @cite{d} are
- distinct numbers.
- X
- @kindex a <
- @kindex a >
- @kindex a [
- @kindex a ]
- @pindex calc-less-than
- @pindex calc-greater-than
- @pindex calc-less-equal
- @pindex calc-greater-equal
- @tindex lt
- @tindex gt
- @tindex leq
- @tindex geq
- @tindex <
- @tindex >
- @tindex <=
- @tindex >=
- The @kbd{a <} (@code{calc-less-than}) [@samp{lt(a,b)} or @samp{a < b}]
- operation is true if @cite{a} is less than @cite{b}. Similar functions
- are @kbd{a >} (@code{calc-greater-than}) [@samp{gt(a,b)} or @samp{a > b}],
- @kbd{a [} (@code{calc-less-equal}) [@samp{leq(a,b)} or @samp{a <= b}], and
- @kbd{a ]} (@code{calc-greater-equal}) [@samp{geq(a,b)} or @samp{a >= b}].
- X
- While the inequality functions like @code{lt} do not accept more
- than two arguments, the syntax @samp{a <= b < c} is translated to an
- equivalent expression involving intervals: @samp{b in [a .. c)}.
- (See the description of @code{in} below.) All four combinations
- of @samp{<} and @samp{<=} are allowed, or any of the four combinations
- of @samp{>} and @samp{>=}. Four-argument constructions like
- @samp{a < b < c < d}, and mixtures like @samp{a < b = c} that
- involve both equalities and inequalities, are not allowed.
- X
- @kindex a .
- @pindex calc-remove-equal
- @tindex rmeq
- The @kbd{a .} (@code{calc-remove-equal}) [@code{rmeq}] command extracts
- the righthand side of the equation or inequality on the top of the
- stack. It also works elementwise on vectors. For example, if
- @samp{[x = 2.34, y = z / 2]} is on the stack, then @kbd{a .} produces
- @samp{[2.34, z / 2]}. As a special case, if the righthand side is a
- variable and the lefthand side is a number (as in @samp{2.34 = x}), then
- Calc keeps the lefthand side instead. Finally, this command works with
- assignments @samp{x := 2.34} as well as equations, always taking the
- the righthand side, and for @samp{=>} (evaluates-to) operators, always
- taking the lefthand side.
- X
- @kindex a &
- @pindex calc-logical-and
- @tindex land
- @tindex &&
- The @kbd{a &} (@code{calc-logical-and}) [@samp{land(a,b)} or @samp{a && b}]
- function is true if both of its arguments are true, i.e., are
- non-zero numbers. In this case, the result will be either @cite{a} or
- @cite{b}, chosen arbitrarily. If either argument is zero, the result is
- zero. Otherwise, the formula is left in symbolic form.
- X
- @kindex a |
- @pindex calc-logical-or
- @tindex lor
- @tindex ||
- The @kbd{a |} (@code{calc-logical-or}) [@samp{lor(a,b)} or @samp{a || b}]
- function is true if either or both of its arguments are true (nonzero).
- The result is whichever argument was nonzero, choosing arbitrarily if both
- are nonzero. If both @cite{a} and @cite{b} are zero, the result is
- zero.
- X
- @kindex a !
- @pindex calc-logical-not
- @tindex lnot
- @tindex !
- The @kbd{a !} (@code{calc-logical-not}) [@samp{lnot(a)} or @samp{!@: a}]
- function is true if @cite{a} is false (zero), or false if @cite{a} is
- true (nonzero). It is left in symbolic form if @cite{a} is not a
- number.
- X
- @kindex a :
- @pindex calc-logical-if
- @tindex if
- @tindex ?
- @tindex :
- @cindex Arguments, not evaluated
- The @kbd{a :} (@code{calc-logical-if}) [@samp{if(a,b,c)} or @samp{a ? b :@: c}]
- function is equal to either @cite{b} or @cite{c} if @cite{a} is a nonzero
- number or zero, respectively. If @cite{a} is not a number, the test is
- left in symbolic form and neither @cite{b} nor @cite{c} is evaluated in
- any way. In algebraic formulas, this is one of the few Calc functions
- whose arguments are not automatically evaluated when the function itself
- is evaluated. The others are @code{lambda}, @code{quote}, and
- @code{condition}.
- X
- One minor surprise to watch out for is that the formula @samp{a?3:4}
- will not work because the @samp{3:4} is parsed as a fraction instead of
- as three separate symbols. Type something like @samp{a ? 3 : 4} or
- @samp{a?(3):4} instead.
- X
- As a special case, if @cite{a} evaluates to a vector, then both @cite{b}
- and @cite{c} are evaluated; the result is a vector of the same length
- as @cite{a} whose elements are chosen from corresponding elements of
- @cite{b} and @cite{c} according to whether each element of @cite{a}
- is zero or nonzero. Each of @cite{b} and @cite{c} must be either a
- vector of the same length as @cite{a}, or a non-vector which is matched
- with all elements of @cite{a}.
- X
- @kindex a @{
- @pindex calc-in-set
- @tindex in
- The @kbd{a @{} (@code{calc-in-set}) [@samp{in(a,b)}] function is true if
- the number @cite{a} is in the set of numbers represented by @cite{b}.
- If @cite{b} is an interval form, @cite{a} must be one of the values
- encompassed by the interval. If @cite{b} is a vector, @cite{a} must be
- equal to one of the elements of the vector. (If any vector elements are
- intervals, @cite{a} must be in any of the intervals.) If @cite{b} is a
- plain number, @cite{a} must be numerically equal to @cite{b}.
- @xref{Set Operations}, for a group of commands that manipulate sets
- of this sort.
- X
- @tindex typeof
- The @samp{typeof(a)} function produces an integer or variable which
- characterizes @cite{a}. If @cite{a} is a number, vector, or variable,
- the result will be one of the following numbers:
- X
- @example
- X 1 Integer
- X 2 Fraction
- X 3 Floating-point number
- X 4 HMS form
- X 5 Rectangular complex number
- X 6 Polar complex number
- X 7 Error form
- X 8 Interval form
- X 9 Modulo form
- 10 Date-only form
- 11 Date/time form
- 12 Infinity (inf, uinf, or nan)
- 100 Variable
- 101 Vector (but not a matrix)
- 102 Matrix
- @end example
- X
- Otherwise, @cite{a} is a formula, and the result is a variable which
- represents the name of the top-level function call.
- X
- @tindex integer
- @tindex real
- @tindex constant
- The @samp{integer(a)} function returns true if @cite{a} is an integer.
- The @samp{real(a)} function
- is true if @cite{a} is a real number, either integer, fraction, or
- float. The @samp{constant(a)} function returns true if @cite{a} is
- any of the objects for which @code{typeof} would produce an integer
- code result except for variables, and provided that the components of
- an object like a vector or error form are themselves constant.
- Note that infinities do not satisfy any of these tests.@refill
- X
- @xref{Declarations}, for a set of similar functions that recognize
- formulas as well as actual numbers. For example, @samp{dint(floor(x))}
- is true because @samp{floor(x)} is provably integer-valued, but
- @samp{integer(floor(x))} does not because @samp{floor(x)} is not
- literally an integer constant.
- X
- @tindex refers
- The @samp{refers(a,b)} function is true if the variable (or sub-expression)
- @cite{b} appears in @cite{a}, or false otherwise. Unlike the other
- tests described here, this function returns a definite ``no'' answer
- even if its arguments are still in symbolic form. The only case where
- @code{refers} will be left unevaluated is if @cite{a} is a plain
- variable (different from @cite{b}).
- X
- @tindex negative
- The @samp{negative(a)} function returns true if @cite{a} ``looks'' negative,
- because it is a negative number, because it is of the form @cite{-x},
- or because it is a product or quotient with a term that looks negative.
- This is most useful in rewrite rules. Beware that @samp{negative(a)}
- evaluates to 1 or 0 for @emph{any} argument @cite{a}, so it can only
- be stored in a formula if the default simplifications are turned off
- first with @kbd{m O}.
- X
- @tindex variable
- The @samp{variable(a)} function is true if @cite{a} is a variable,
- or false if not. If @cite{a} is a function call, this test is left
- in symbolic form. Built-in variables like @code{pi} and @code{inf}
- are considered variables like any others by this test.
- X
- @tindex nonvar
- The @samp{nonvar(a)} function is true if @cite{a} is a non-variable.
- If its argument is a variable it is left unsimplified; it never
- actually returns zero. However, since Calc's condition-testing
- commands consider ``false'' anything not provably true, this is
- often good enough.
- X
- @tindex lin
- @tindex linnt
- @tindex islin
- @tindex islinnt
- @cindex Linearity testing
- The functions @code{lin}, @code{linnt}, @code{islin}, and @code{islinnt}
- check if an expression is ``linear,'' i.e., can be written in the form
- @cite{a + b x} for some constants @cite{a} and @cite{b}, and some
- variable or subformula @cite{x}. The function @samp{islin(f,x)} checks
- if formula @cite{f} is linear in @cite{x}, returning 1 if so. For
- example, @samp{islin(x,x)}, @samp{islin(-x,x)}, @samp{islin(3,x)}, and
- @samp{islin(x y / 3 - 2, x)} all return 1. The @samp{lin(f,x)} function
- is similar, except that instead of returning 1 it returns the vector
- @cite{[a, b, x]}. For the above examples, this vector would be
- @cite{[0, 1, x]}, @cite{[0, -1, x]}, @cite{[3, 0, x]}, and
- @cite{[-2, y/3, x]}, respectively. Both @code{lin} and @code{islin}
- generally remain unevaluated for expressions which are not linear,
- e.g., @samp{lin(2 x^2, x)} and @samp{lin(sin(x), x)}. The second
- argument can also be a formula; @samp{islin(2 + 3 sin(x), sin(x))}
- returns true.
- X
- The @code{linnt} and @code{islinnt} functions perform a similar check,
- but require a ``non-trivial'' linear form, which means that the
- @cite{b} coefficient must be non-zero. For example, @samp{lin(2,x)}
- returns @cite{[2, 0, x]} and @samp{lin(y,x)} returns @cite{[y, 0, x]},
- SHAR_EOF
- true || echo 'restore of calc.texinfo failed'
- fi
- echo 'End of part 47'
- echo 'File calc.texinfo is continued in part 48'
- echo 48 > _shar_seq_.tmp
- exit 0
- exit 0 # Just in case...
- --
- Kent Landfield INTERNET: kent@sparky.IMD.Sterling.COM
- Sterling Software, IMD UUCP: uunet!sparky!kent
- Phone: (402) 291-8300 FAX: (402) 291-4362
- Please send comp.sources.misc-related mail to kent@uunet.uu.net.
-